import QtQuick 2.1
import QtQuick.Controls 1.1
import QtQuick.Controls.Styles 1.1
import SmartingEEGClientAgent 1.0

Item
{
    id: connectPage
    anchors.fill: parent

    Row
    {
        id: btnRow

        property double heightCoeff: 0.2
        property double marginCoeff: 0.05
        property double spacingCoeff: 0.05

        property var btnWidth: width * (1 - spacingCoeff) / 2
        property var btnHeight: height * (1 - spacingCoeff) / 2

        anchors.top: parent.top
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.topMargin: marginCoeff * parent.height
        anchors.bottomMargin: marginCoeff * parent.height
        anchors.leftMargin: marginCoeff * parent.width
        anchors.rightMargin: marginCoeff * parent.width

        height: heightCoeff * parent.height
        spacing: spacingCoeff * parent.width

        Button
        {
            id: scanBtn
            width: parent.btnWidth
            height: parent.btnHeight

            text: "Scan"
            visible: true
            style: touchStyle
            onClicked:
            {
                plotView.newRecord();
                eegAgent.stopServerDiscovery();
                deviceModel.clear();
                eegAgent.startServerDiscovery();
            }
        }

        Button
        {
            id: connectBtn
            width: parent.btnWidth
            height: parent.btnHeight

            text: "Connect"
            visible: true
            style: touchStyle
            onClicked:
            {
                if(deviceView.currentIndex >= 0)
                {
                    eegAgent.stopServerDiscovery()
                    eegAgent.connectToServer(deviceModel.get(deviceView.currentIndex).address)
                    deviceModel.clear()
                }
            }
        }

        Button
        {
            id: disconnectBtn
            width: parent.btnWidth
            height: parent.btnHeight

            text: "Disconnect"
            visible: false
            style: touchStyle
            onClicked:
            {
                eegAgent.disconnectFromServer();
                eegAgent.onDisconnectedFromServer();
                plotView.closeRecord();
            }
        }


        Button
        {
            id: acquisitionBtn
            width: parent.btnWidth
            height: parent.btnHeight

            text: "Acquisition"
            visible: false
            style: touchStyle
            onClicked:
            {
                plotView.newRecord();
                /*for(var i = 0; i < 24; ++i)
                {
                    if(plotView.isVisible(i + 4))
                    {
                        eegAgent.enableChannel(i);
                    }else
                    {
                        eegAgent.disableChannel(i);
                    }
                }
                eegAgent.setMode(SmartingEEGClientAgent.NORMAL_MODE)
                eegAgent.enableImpedanceMeasurement();*/
                eegAgent.startAcquisition()
            }
        }
    }

    Component
    {
        id: touchStyle
        ButtonStyle {
            panel: Item
            {
                implicitHeight: 70
                implicitWidth: 140
                BorderImage
                {
                    anchors.fill: parent
                    antialiasing: true
                    border.bottom: 8
                    border.top: 8
                    border.left: 8
                    border.right: 8
                    anchors.margins: control.pressed ? -4 : 0
                    source: control.pressed ? "images/button_pressed.png" : "images/button_default.png"
                    Text {
                        text: control.text
                        anchors.centerIn: parent
                        color: "white"
                        font.pixelSize: 23
                        renderType: Text.NativeRendering
                    }
                }
            }
        }
    }

    //ListView of detected devices
    ListModel
    {
        id: deviceModel
    }

    Connections
    {
        target: eegAgent

        onServerDiscovered:
        {
            console.log("Server Discovered: " + name);
            deviceModel.append({"name": name, "address": address})
        }

        onConnectedToServer:
        {
            scanBtn.visible = false;
            connectBtn.visible = false;
            disconnectBtn.visible = true;
            acquisitionBtn.visible = true;
            deviceView.visible = false;

            console.log("CONNECTED");
        }

        onDisconnectedFromServer:
        {
            scanBtn.visible = true;
            connectBtn.visible = true;
            disconnectBtn.visible = false;
            acquisitionBtn.visible = false;
            deviceView.visible = true;

            console.log("DISCONNECTED");
        }
    }

    ListView
    {
        id: deviceView
        anchors.top: btnRow.bottom
        anchors.bottom: parent.bottom
        anchors.left: parent.left
        anchors.right: parent.right
        model: deviceModel
        delegate: Component
        {
            id: deviceDelegate

            Item
            {
                id: deviceDelegateItem
                width: parent.width
                height: col.height

                Column
                {
                    id: col
                    spacing: 3
                    anchors.margins: 15

                    Text
                    {
                        id: nameTxt
                        text: "<b>Name:</b> " + name
                        color: "white"
                        font.pixelSize: 23
                    }

                    Text
                    {
                        id: addressTxt
                        text: "<b>Address:</b> " + address
                        color: "white"
                        font.pixelSize: 23
                    }

                    Rectangle
                    {
                        anchors.left: parent.left
                        anchors.right: parent.right
                        anchors.margins: 15
                        height: 1
                        color: "#424246"
                    }
                }

                MouseArea
                {
                    anchors.fill: parent
                    onClicked:
                    {
                        deviceView.currentIndex = index
                    }
                }
            }
        }
        highlight: Rectangle {color: "#40ffffff"}
    }
    }
